﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace VideoTools.Controls
{
    public class ListBoxLineUI : Grid
    {

        // 控件构造函数
        public ListBoxLineUI()
        {
            // 在这里可以手动设置DonePercent的值，而不是通过绑定
            //this.DonePercent = 0d;
        }
        // 注册为依赖项属性
        public static readonly DependencyProperty DonePercentProperty =
            DependencyProperty.Register("DonePercent", typeof(Double), typeof(ListBoxLineUI), new PropertyMetadata(0.0, OnDonePercentChanged));


        // 注册为依赖项属性
        public static readonly DependencyProperty DoneBackgroundProperty =
            DependencyProperty.Register("DoneBackground", typeof(System.Windows.Media.Color), typeof(ListBoxLineUI), new PropertyMetadata(Colors.PaleTurquoise));


        private static void OnDonePercentChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var customGrid = (ListBoxLineUI)sender;
            double newValue = (double)e.NewValue;
            // 在这里添加响应 DonePercent 变化的逻辑
            //Debug.WriteLine("++++++++ " + newValue);
            customGrid.InvalidateVisual(); // 触发重绘
        }

        // 当MyProperty改变时，触发PropertyChanged事件
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            // 假设MyProperty在这里有一些变化的逻辑
            // 这里只是一个示例，实际的逻辑根据需要编写
            //MyProperty = "New Value";
        }
        public System.Windows.Media.Color DoneBackground
        {
            get { return (System.Windows.Media.Color)GetValue(DoneBackgroundProperty); }
            set
            {
                SetValue(DoneBackgroundProperty, value);
            }
        }
        public Double DonePercent 
        {
            get { return (Double)GetValue(DonePercentProperty); }
            set 
            {
                SetValue(DonePercentProperty, value);
            }
        }
        protected override void OnRender(DrawingContext drawingContext)
        {
            base.OnRender(drawingContext);
            //Debug.WriteLine("DonePercent" + DonePercent);
            if(DonePercent > 0)
            {
                double actualWidth = this.ActualWidth;
                double backgroundWidth = actualWidth * DonePercent;
                Rect rect = new Rect(0, 0, backgroundWidth, this.ActualHeight);
                drawingContext.DrawRectangle(new SolidColorBrush(DoneBackground),null, rect);
            }
        }
    }
}
